package com.wzy.Jdbc封装.service.Impl;

import com.wzy.Jdbc封装.dao.NewsDao;
import com.wzy.Jdbc封装.pojo.News_user;
import com.wzy.Jdbc封装.service.option_News_user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class option_News_userImpl implements option_News_user {
    @Override
    public List<News_user> selectById(News_user user) throws SQLException {
        ResultSet resultSet =null;
        ArrayList<News_user> list = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_user` WHERE id=?";
            Object[] obj={user};
            resultSet = NewsDao.selectById3(sql, obj);
            while(resultSet.next()) {
                News_user news_user = new News_user();
                int id1 = resultSet.getInt("id");
                String userName = resultSet.getString(2);
                String password = resultSet.getString(3);
                String email = resultSet.getString(4);
                int userType = resultSet.getInt(5);
                news_user.setId(id1);
                news_user.setUserName(userName);
                news_user.setPassword(password);
                news_user.setEmail(email);
                news_user.setUserType(userType);
                list.add(news_user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            resultSet.close();
        }
        return list;
    }

    @Override
    public int deleteById(News_user user) throws SQLException {
        int rows = 0;
        String sql = "DELETE FROM `news_user`  WHERE id=? ";
        Object[] obj = {user};
        rows = NewsDao.delete3(sql, obj);
        return rows;
    }

    @Override
    public int updateById(News_user user) {
        int rows = 0;
        String sql = "UPDATE `news_user` SET password=? WHERE id=?";
        Object[] obj = {user};
        rows = NewsDao.update3(sql, obj);
        return rows;
    }

    @Override
    public int insertNews(News_user user) {
        int rows = 0;
        String sql = "INSERT INTO `newsdb`.`news_user` (`id`, `userName`, `password`, `email`, `userType`) VALUES (?, ?, ?, ?, ?)";
        Object[] obj = {user};
        rows = NewsDao.insert3(sql, obj);
        return rows;
    }
}
